<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../node_modules/vue/dist/vue.js"></script>
</head>
<body>
    <div id="app">
        <div>父组件</div>
        <div>
            <button @click ='handle'>销毁事件</button>
        </div>
        <test-tom></test-tom>
        <test-jerry></test-jerry>
    </div>
    <script>
        /* 提供事件中心 */
        var eventHub = new Vue();


        var vm = new Vue({
            el:'#app',
            data:{
            
            },
            methods: {
                handle(){
                    // 销毁事件
                    eventHub.$off('tom-event');
                    eventHub.$off('jerry-event');
                }
            },
            
            components:{
                TestTom:{
                    data:function(){
                        return {
                            num:0
                        }
                    },
                    template:`
                        <div>
                            <div>TOM:{{num}}</div>
                            <div>
                                <button @click='handle'>点击</button>    
                            </div>
                        </div>
                    `,
                    methods: {
                        handle(){
                            // 触发事件
                            eventHub.$emit('jerry-event',1);
                        }
                    },
                    mounted() {
                        // 监听事件
                        eventHub.$on('tom-event',(val)=>{
                            this.num +=val;
                        })
                    },
                },
                TestJerry: {
                    data: function () {
                        return {
                            num: 0
                        }
                    },
                    template: `
                        <div>
                            <div>Jerry:{{num}}</div>
                            <div>
                                <button @click='handle'>点击</button>    
                            </div>
                        </div>
                    `,
                    methods: {
                        handle(){
                            eventHub.$emit('tom-event', 2);
                        }
                    },
                    mounted() {
                        eventHub.$on('jerry-event',(val)=>{
                            this.num += val;
                        });
                    }
                }
            }
        })
    </script>
</body>
</html>